package com.huazi.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huazi.commonutils.CommentResult;
import com.huazi.eduservice.entity.EduCourse;
import com.huazi.eduservice.entity.EduSubject;
import com.huazi.eduservice.service.EduSubjectService;
import com.huazi.eduservice.vo.Options;
import com.huazi.eduservice.vo.SubjectInfo;
import com.huazi.eduservice.vo.TreeData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-03-17
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/edu-subject")
public class EduSubjectController {
    @Autowired
    EduSubjectService subjectService;
    //接收上传过来的文件
    @PostMapping("addSubject")
    public CommentResult addSubject( MultipartFile file){
        subjectService.saveSubject(file,subjectService);
        return CommentResult.getOK();
    }
    //获取树形数据
    @GetMapping("getTreeSubject")
    public CommentResult getTreeSubject(){
        List<TreeData> treeList =new ArrayList<>();
        List<TreeData> trees = getTrees(treeList, "0");
        return CommentResult.getOK().data("data",trees);
    }
    //传入list来接收子集(递归获取数据)
    public List<TreeData> getTrees(List<TreeData> list, String pid){
        List<EduSubject> parent_id = subjectService.list(new QueryWrapper<EduSubject>().eq("parent_id", pid));
        if(parent_id!=null&&parent_id.size()>0){//如果查询到数据则进行添加本级
            for (EduSubject ed: parent_id
            ) {
                TreeData treeData=new TreeData();//创建前端数据实体
                //赋值
                treeData.setId(ed.getSubId()).setTitle(ed.getTitle());
                //创建一个list进行传入，如果有数据就会添加到treeData的tree子级集合中
                List<TreeData> list1=new ArrayList<>();
                getTrees(list1,ed.getSubId());
                if(list1!=null){//判断是否还有子级
                    treeData.setChildren(list1);//添加子级
                }
                list.add(treeData);//添加本级
            }
        }
        return list;
    }


    //根据parent_id查询所有分类
    @GetMapping("getSubjects/{pid}")
    public CommentResult getSubjects(@PathVariable String pid){
        List<EduSubject> parent_id = subjectService.list(new QueryWrapper<EduSubject>().eq("parent_id", pid));
        if(parent_id!=null&&parent_id.size()>0){
            Map<String,Object> map=new ConcurrentHashMap<>();
            for (EduSubject cou: parent_id
            ) {
                map.put(cou.getSubId(),cou.getTitle());
            }
//            System.out.println("subjects");
            return CommentResult.getOK().data("subjects",map);
        }
        return CommentResult.getOK().data("subjects","");
    }
    //获取所有2级课程类别信息
    @GetMapping("getSubjectInfo")
    public CommentResult getSubjectInfo(){
        List<SubjectInfo> subjectInfo = subjectService.getSubjectInfo();
        return CommentResult.getOK().data("subjectInfos",subjectInfo);
    }
    //获取一级分类学习
    @GetMapping("getFrontSubjects/{pid}")
    public CommentResult getFrontSubjects(@PathVariable("pid") String pid){
        List<EduSubject> subjects = subjectService.list(new QueryWrapper<EduSubject>().eq("parent_id", pid));
        return CommentResult.getOK().data("subjects",subjects);

    }
}

